package com.jie.langlangmall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.constant.AttributeType;
import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.dao.PmsProductAttributeDao;
import com.jie.langlangmall.dto.PmsProductAttributeParam;
import com.jie.langlangmall.dto.ProductAttrInfo;
import com.jie.langlangmall.mapper.PmsProductAttributeCategoryMapper;
import com.jie.langlangmall.mapper.PmsProductAttributeMapper;
import com.jie.langlangmall.mapper.PmsProductAttributeValueMapper;
import com.jie.langlangmall.model.PmsProductAttribute;
import com.jie.langlangmall.model.PmsProductAttributeCategory;
import com.jie.langlangmall.model.PmsProductAttributeExample;
import com.jie.langlangmall.model.PmsProductAttributeValueExample;
import com.jie.langlangmall.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/23
 * @Email 3217998214@qq.com
 * @Description 商品属性管理
 */
@Service
public class PmsProductAttributeServiceImpl implements PmsProductAttributeService {

    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;
    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private PmsProductAttributeDao productAttributeDao;
    @Autowired
    private PmsProductAttributeValueMapper productAttributeValueMapper;

    /**
     * 根据分类查询属性或者参数列表
     *
     * @param cid
     * @param type
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<PmsProductAttribute> list(Long cid, Integer type, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PmsProductAttributeExample example = new PmsProductAttributeExample();
        example.setOrderByClause("sort desc");
        // type是0代表规格，1代表参数
        // select * from pms_product_attribute where product_attribute_category_id = ? and type = ?
        example.createCriteria().andProductAttributeCategoryIdEqualTo(cid).andTypeEqualTo(type);
        return productAttributeMapper.selectByExample(example);
    }

    /**
     * 根据ID查询属性的详情
     *
     * @param id
     * @return
     */
    @Override
    public PmsProductAttribute getItem(Long id) {
        return productAttributeMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新属性信息
     *
     * @param id
     * @param productAttributeParam
     */
    @Override
    public void update(Long id, PmsProductAttributeParam productAttributeParam) {
        PmsProductAttribute productAttribute = BeanUtil.copyProperties(productAttributeParam, PmsProductAttribute.class);
        productAttribute.setId(id);
        productAttributeMapper.updateByPrimaryKeySelective(productAttribute);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        // 如果属性关联了商品，不能删除
        PmsProductAttributeValueExample productAttributeValueExample = new PmsProductAttributeValueExample();
        productAttributeValueExample.createCriteria().andProductAttributeIdIn(ids);
        if (productAttributeValueMapper.countByExample(productAttributeValueExample) > 0) {
            throw new ApiException(MessageConstant.ATTRIBUTE_CONNECT_PRODUCT);
        }

        // 获取这些属性是属于哪个属性分组下的
        PmsProductAttribute productAttribute = productAttributeMapper.selectByPrimaryKey(ids.get(0));
        Integer type = productAttribute.getType();
        // 查询对应的分组
        PmsProductAttributeCategory productAttributeCategory =
                productAttributeCategoryMapper.selectByPrimaryKey(productAttribute.getProductAttributeCategoryId());
        // 删除属性
        PmsProductAttributeExample example = new PmsProductAttributeExample();
        example.createCriteria().andIdIn(ids);
        int count = productAttributeMapper.deleteByExample(example);

        if (type == AttributeType.ATTRIBUTE.getCode()) {
            // 属性
            if (productAttributeCategory.getAttributeCount() >= count) {
                productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() - count);
            } else {
                productAttributeCategory.setAttributeCount(0);
            }
        } else if (type == AttributeType.PARAM.getCode()) {
            // 规格
            if (productAttributeCategory.getParamCount() >= count) {
                productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() - count);
            } else {
                productAttributeCategory.setParamCount(0);
            }
        }
        productAttributeCategoryMapper.updateByPrimaryKey(productAttributeCategory);
    }

    /**
     * 获取商品分类对应属性列表
     *
     * @param productCategoryId
     * @return
     */
    @Override
    public List<ProductAttrInfo> getProductAttrInfo(Long productCategoryId) {
        return productAttributeDao.getProductAttrInfo(productCategoryId);
    }

    /**
     * 新增商品属性
     *
     * @param productAttributeParam
     */
    @Override
    public void create(PmsProductAttributeParam productAttributeParam) {
        // 1. 保存到数据库里面
        PmsProductAttribute productAttribute = BeanUtil.copyProperties(productAttributeParam, PmsProductAttribute.class);
        int count = productAttributeMapper.insertSelective(productAttribute);
        if (count < 0) {
            throw new ApiException(MessageConstant.INSERT_FAILED);
        }
        // 2. 新增商品属性以后，需要更新该属性所属的属性分组的属性数量
        // select * from pms_attribute_category where id = ?
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectByPrimaryKey(productAttribute.getProductAttributeCategoryId());
        if (productAttributeCategory == null) {
            throw new ApiException(MessageConstant.ATTRIBUTE_CATEGORY_NOT_EXIST);
        }

        if (productAttribute.getType() == AttributeType.ATTRIBUTE.getCode()) {
            // 属性
            productAttributeCategory.setAttributeCount((productAttributeCategory.getAttributeCount() == null ? 0 : productAttributeCategory.getAttributeCount()) + 1);
        } else if (productAttribute.getType() == AttributeType.PARAM.getCode()) {
            // 规格
            productAttributeCategory.setParamCount((productAttributeCategory.getParamCount() == null ? 0 : productAttributeCategory.getParamCount()) + 1);
        }
        productAttributeCategoryMapper.updateByPrimaryKey(productAttributeCategory);
    }
}
